<!-- （张仕琪）配送地址列表页面 -->
<template>
  <div>
    <van-nav-bar title="确认收货地址" placeholder fixed left-arrow @click-left="goBack" />
    <van-address-list v-model="chosenAddressId" :list="addressList" default-tag-text="默认" @add="onAdd" @edit="onEdit" />
  </div>

  <!-- <van-address-list
  v-model="chosenAddressId"
  :list="list"
  :disabled-list="disabledList"
  disabled-text="以下地址超出配送范围"
  default-tag-text="默认"
  @add="onAdd"
  @edit="onEdit"
        /> -->
</template>

<script setup>

import { ref, reactive, defineProps } from 'vue';
import { Toast } from 'vant';
import { useRouter, useRoute } from "vue-router";
import http from '@/utils/request.js'
import { getHeadImageUrl } from '@/utils/api.js'
import { deepCopy } from '@/utils/object.js';
const chosenAddressId = ref(1);
const router = useRouter();

const goBack = () => history.back();

//拿路由传的id
const props = defineProps(['userId'])
const address = reactive({ userId: props.userId, })
const addressList = reactive([]);

const getAddressList = function () {
  http
    .get(
      "/user/address/" + address.userId //将取到的userId与路径组合
    )
    .then((res) => {
      console.log('获取到的列表：', res.data);
      //deepCopy(addressList, res.data.data);
      res.data.data.userAddresses.forEach(item => {
        const addressMes = {
          id: item.addressId,
          name: item.userName,
          tel: item.userTel,
          address: item.userProvince+item.userCity+item.userDistrict+item.userAddress,
          isDefault: item.addressDefault == 1,
        }
        addressList.push(addressMes);
      });
      console.log('获取当前的地址的信息【address】：', addressList);
    })
};

getAddressList();
console.log("---------" + addressList.userName);
const list = [
  // {
  //   id: address.addressId,
  //   name: address.userName,
  //   tel: address.userTel,
  //   address: address.userProvince,
  //   isDefault: true,
  // },
  {
    id: '2',
    name: '李四',
    tel: '1310000000',
    address: '浙江省杭州市拱墅区莫干山路 50 号',
  },
];

//传用户的id给地址增加页面
const onAdd = () => router.push("/addressadd/"+address.userId);
//Toast('新增地址');
//修改地址
const onEdit = (item, index) => router.push("/addressedit/"+addressList[index].id);
//Toast('编辑地址:' + addressList[index].id);

// const onEdit = (item, index) =>function toEidtAddressitem() {

//     router.push("/addressedit");
// }
// export default {
//   data() {
//     return {
//       uname: sessionStorage.getItem("uname"), //获取用户名
//       chosenAddressId: "1",
//       list: [],
//     };
//   },
//   methods: {
//     goBack() {
//       this.$router.go(-1);
//     },
//     onAdd() {
//       this.$router.push("/address");
//     },
//     //点击编辑传地址id给地址编辑，以便地址回显
//     onEdit(item, index) {
//       let id = this.list[index].id;
//       this.$router.push(`address?id=${id}`);
//     },
//   },
//   mounted() {
//     //发送请求获取地址
//     this.axios.post("/address?uname=" + this.uname).then((res) => {
//       this.list = res.data.results;
//       //选中地址列表的第一个地址
//       if (this.list.length != 0) {
//         this.chosenAddressId = this.list[0].id;
//       }
//       console.log(this.list);
//     });
//   },
// };
</script>

